package com.zte.springboot_smbms.controller;

import com.zte.springboot_smbms.pojo.SmbmsBill;
import com.zte.springboot_smbms.service.BillsService;
import com.zte.springboot_smbms.service.ProvidersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/billController")
public class BillController {
    @Autowired
    private BillsService billsService;
    @Autowired
    private ProvidersService providersService;

    @RequestMapping("/html_billadd")
    public String html_billadd(){
        return "billadd";
    }

    @RequestMapping("/findAllBill")
    public String findAllBill(Model model){
        List<SmbmsBill> allBill = billsService.findAllBill();
        System.out.println(allBill.get(0).getCreationdate().toString());
        model.addAttribute("billList",allBill);
        return "billlist";
    }

    @RequestMapping("/findBillByParam")
    public String findBillByParam(Model model,String queryProductName,Integer queryProviderId,Integer queryIsPayment){
        model.addAttribute("billList",billsService.findBillByParam(queryProductName,queryProviderId,queryIsPayment));
        return "billlist";
    }

    @RequestMapping("/findBillById")
    public String findBillById(Model model,Long id,String method){
        if ("viewbill".equals(method)){
            model.addAttribute("bill",billsService.findBillById(id).get(0));
            model.addAttribute("providerliset",providersService.findAllProvider());
            return "billview";
        }
        if ("modifybill".equals(method)) {
            model.addAttribute("bill",billsService.findBillById(id).get(0));
            model.addAttribute("providerliset",providersService.findAllProvider());
            return "billmodify";
        }
        return "";
    }

    @RequestMapping("/delBillById")
    public String delBillById(Model model,Long id){
        boolean b = billsService.delBill(id);
        if (b) {
            return "forward:findAllBill";
        }
        else {
            return "billlist";
        }
    }

    @RequestMapping("/addBill")
    public String addBill(SmbmsBill bill ,Model model){
        boolean b = billsService.addBill(bill);
        if (b) {
            return "forward:findAllBill";
        } else {
            return "billlist";
        }
    }

    @RequestMapping("/updateBillById")
    public String updateBillById(SmbmsBill bill ,Model model){
        boolean b = billsService.updateBill(bill.getId(),bill);
        if (b) {
            return "forward:findAllBill";
        } else {
            return "billlist";
        }
    }

}
